<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/css/my_log_register.css">
    <style type="text/css">
        /* 眼睛样式 */
        #eyes {
            width: 18px;
            position: absolute;
            top: 12px;
            right: 2.5rem;
            float: left;
        }
     </style>
</head>

<body>
    <!-- 提示框 -->
    <div class="col">
        <div class="alert alert-danger alert-dismissible fade show invisible text-center" style="width: 30rem;" role="alert">
            <strong>提示：</strong><label for="" id="msg">有错误！！！</label>
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
    </div>

    <div id="container" class="shadow p-3 mb-5 bg-white rounded">
        <div id="title" style="margin-bottom: 2rem;">
            <h1 style="text-align: center; padding-top: 1rem;">登陆</h1>
        </div>

        <form id="loginForm" action="/loginDone" method="POST">
            <!-- 文本框 -->
            <div id="body_middle">
                <!-- 账号框 -->
                <div class="row">
                  <div class="col">
                      <center>
                          <input type="text" class="form-control" id="username" name="username" placeholder="用户名/手机号/邮箱" style="width: 50%; text-align: center;">
                        </center>
                    </div>
                </div>
                <!-- 密码框 -->
                <div class="row">
                    <div class="col">
                        <center>
                            <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码" style="width: 50%; text-align: center">
                            <!-- 功能小眼睛 -->
                            <img src="../static/image/eye-slash.svg" alt="" id="eyes" style="margin-right: 6.6rem; margin-top: 0px; ">
                        </center>
                    </div>
                </div>
            </div>
            
            <!-- 辅助板块 -->
            <div>
                <div class="row">
                    <div class="col">
                        
                    </div>
                </div>
                <!-- 按钮组件 -->
                <div style="text-align: center;">
                    <center></center>
                    <button type="button" class="btn btn-outline-success" style="width: 14rem;" onclick="jumpTo()">登录</button> 
                </div>
                <div class="form-group btn" style="margin-left: 2rem;">
                    <a href="../view/register.html" type="button" onclick="jump()">注册账号</a>
                </div>
            </div>
        </form>
    </div>
    
    <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
    <script src="../static/js/jquery-3.5.1.min.js"></script>
    <script src="../static/js/bootstrap.min.js"></script>
    <!-- 登录页输入框焦点逻辑 -->
    <script src="../static/js/login_input_focus.js"></script>
    <!-- 小眼睛——显示隐藏密码实现逻辑 -->
    <script src="../static/js/icon_click.js"></script>
  

    <script>
        function jump() {
            window.location.href = "/register";
        }

        function jumpTo() {
            let username = $('#username').val();
            let password = $('#password').val();

            if (username.length > 0 && password.length > 0) {
                axios({
                    method: "post",
                    url: "/loginDone",
                    data: $("#loginForm").serialize()
                }).then(function (res) {
                    // axios工具下，服务器真正返回的数据在返回对象的data中
                    res = res.data;
                    if (res.Code === 200) {
                        window.location.href = "/";
                    } else {
                        alert_fn(res.msg)
                    }
                }).catch((err) => {
                    alert_fn(err),
                        console.log(err);
                })
            } else {
                alert_fn("用户名、密码不能为空")
            }
        }
        // 这个方法包装了alert的相关行为和显示的内容
        function alert_fn(msg) {
            msg = msg || '密码不能为空，两次密码应该一致';
            // 替换提示的内容
            $('#msg').text(msg);
            // 移除不显示的class，则alert将显示
            $('.alert').removeClass('invisible');
            // 三秒中后，给alert增加不显示的class，则alert重新隐藏
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 3000);
        }
    </script>    
</body>
</html>